package com.pd.backend.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pd.backend.dtos.PdGoodsTypeDto;
import com.pd.backend.dtos.PdGoodsTypePageDto;
import com.pd.backend.mapper.PdTruckTypeGoodsTypeMapper;
import com.pd.backend.vo.GoodsTypeAndTruckTypeVo;
import com.pd.backend.dtos.PageResponseResult;
import com.pd.backend.dtos.ResponseResult;
import com.pd.backend.enums.AppHttpCodeEnum;
import com.pd.backend.mapper.PdGoodsTypeMapper;
import com.pd.backend.mapper.PdTruckTypeMapper;
import com.pd.backend.pojo.PdGoodsType;
import com.pd.backend.pojo.PdTruckType;
import com.pd.backend.pojo.PdTruckTypeGoodsType;
import com.pd.backend.service.PdGoodsTypeService;
import com.pd.backend.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 货物类型业务实现
 */
@Service
@Transactional
@Slf4j
public class PdGoodsTypeServiceImpl implements PdGoodsTypeService {
    @Autowired
    private PdGoodsTypeMapper pdGoodsTypeMapper;
    @Autowired
    private PdTruckTypeMapper pdTruckTypeMapper;
    @Autowired
    private PdTruckTypeGoodsTypeMapper pdTruckTypeGoodsTypeMapper;
    @Autowired
    private IdWorker idWorker;
    @Override
    public ResponseResult addGoodType(PdGoodsTypeDto pdGoodsTypeDto) {
        //校验参数
        if (Objects.isNull(pdGoodsTypeDto)
                || Objects.isNull(pdGoodsTypeDto.getName())
                || Objects.isNull(pdGoodsTypeDto.getTruckTypeIds())
                || Objects.isNull(pdGoodsTypeDto.getDefaultWeight())
                || Objects.isNull(pdGoodsTypeDto.getDefaultVolume())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询车辆类型是否存在
        List<PdTruckType> pdTruckTypes = pdTruckTypeMapper.selectById(pdGoodsTypeDto.getTruckTypeIds());
        if (Objects.isNull(pdTruckTypes) || pdGoodsTypeDto.getTruckTypeIds().size() != pdTruckTypes.size()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询货物类型名称是否存在
        List<PdGoodsType> pdGoodsTypes = pdGoodsTypeMapper.selectByName(pdGoodsTypeDto.getName());
        //存在
        if (pdGoodsTypes.size()>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        PdGoodsType pdGoodsType = new PdGoodsType();
        BeanUtils.copyProperties(pdGoodsTypeDto,pdGoodsType);
        String pdGoodsTypeId = String.valueOf(idWorker.nextId());
        pdGoodsType.setStatus(1);
        pdGoodsType.setId(pdGoodsTypeId);
        //将商品类型插入数据库
        pdGoodsTypeMapper.save(pdGoodsType);
        List<String> truckTypeIds = pdGoodsTypeDto.getTruckTypeIds();
        for (String truckTypeId : truckTypeIds) {
            PdTruckTypeGoodsType pdTruckTypeGoodsType = new PdTruckTypeGoodsType();
            pdTruckTypeGoodsType.setId(String.valueOf(idWorker.nextId()));
            pdTruckTypeGoodsType.setGoodsTypeId(pdGoodsTypeId);
            pdTruckTypeGoodsType.setTruckTypeId(truckTypeId);
            pdTruckTypeGoodsTypeMapper.savePdTruckTypeGoodsType(pdTruckTypeGoodsType);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询货物类型
     * @param pdGoodsTypePageDto
     * @return
     */
    @Override
    public PageResponseResult page(PdGoodsTypePageDto pdGoodsTypePageDto) {
        //校验参数若pageRequestDto为空赋给默认值
        pdGoodsTypePageDto.getPageRequestDto().checkParam();
        String id = pdGoodsTypePageDto.getTruckTypeId();
        String name = pdGoodsTypePageDto.getGoodsTypeName();
        Page<PdGoodsType> page = PageHelper.startPage(pdGoodsTypePageDto.getPageRequestDto().getPage(), pdGoodsTypePageDto.getPageRequestDto().getSize());
        List<PdGoodsType> pdGoodsTypes = pdGoodsTypeMapper.selectByNameAndTruckTypeId(name, id);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setSize(page.getPageSize());
        pageResponseResult.setCurrentPage(page.getPageNum());
        pageResponseResult.setTotal((int) page.getTotal());
        pageResponseResult.setData(pdGoodsTypes);
        pageResponseResult.setCode(200);
        return pageResponseResult;
    }

    /**
     * 根据id删除货物类型
     * @param id
     * @return
     */

    @Override
    public ResponseResult deleteById(String id) {
        //校验参数
        if (StringUtils.isBlank(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id删除数据
        int result = pdGoodsTypeMapper.deleteById(id);
        if (result < 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //删除关联的车辆类型
        pdTruckTypeGoodsTypeMapper.deleteByGoodsTypeId(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id
     * @param id
     * @return
     */
    @Override
    public ResponseResult<GoodsTypeAndTruckTypeVo> selectGoodsTypeAndTruckTypeByGoodsTypeId(String id) {
        //校验参数
        if (StringUtils.isBlank(id)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //查询货物类型
        PdGoodsType pdGoodsType = pdGoodsTypeMapper.selectAllById(id);
        if (Objects.isNull(pdGoodsType)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (pdGoodsType.getStatus() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_STATUS_EX);
        }
        //查询关联的车辆类型
        List<PdTruckType> pdTruckTypes = pdTruckTypeMapper.selectTruckTypesByGoodsTypeId(id);
        //封装数据
        if (Objects.isNull(pdTruckTypes)){
            GoodsTypeAndTruckTypeVo goodsTypeAndTruckTypeVo = new GoodsTypeAndTruckTypeVo();
            BeanUtils.copyProperties(pdGoodsType,goodsTypeAndTruckTypeVo);
            return ResponseResult.okResult(goodsTypeAndTruckTypeVo);
        }
        GoodsTypeAndTruckTypeVo goodsTypeAndTruckTypeVo = new GoodsTypeAndTruckTypeVo();
        BeanUtils.copyProperties(pdGoodsType,goodsTypeAndTruckTypeVo);
        goodsTypeAndTruckTypeVo.setPdTruckTypes(pdTruckTypes);

        return ResponseResult.okResult(goodsTypeAndTruckTypeVo);
    }

    /**
     * 修改货物类型
     * @param pdGoodsTypeDto
     * @return
     */
    @Override
    public ResponseResult updateGoodsType(PdGoodsTypeDto pdGoodsTypeDto) {
        //检验参数
        if (Objects.isNull(pdGoodsTypeDto)||
            Objects.isNull(pdGoodsTypeDto.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改货物类型
        int result =  pdGoodsTypeMapper.updateById(pdGoodsTypeDto);
        if (result < 1 ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id删除关联的车辆类型id
        pdTruckTypeGoodsTypeMapper.deleteByGoodsTypeId(pdGoodsTypeDto.getId());
        //添加货物类型和车辆类型的关系
        List<String> truckTypeIds = pdGoodsTypeDto.getTruckTypeIds();
        for (String truckTypeId : truckTypeIds) {
            PdTruckTypeGoodsType pdTruckTypeGoodsType = new PdTruckTypeGoodsType();
            pdTruckTypeGoodsType.setId(String.valueOf(idWorker.nextId()));
            pdTruckTypeGoodsType.setGoodsTypeId(pdGoodsTypeDto.getId());
            pdTruckTypeGoodsType.setTruckTypeId(truckTypeId);
            pdTruckTypeGoodsTypeMapper.savePdTruckTypeGoodsType(pdTruckTypeGoodsType);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}